﻿using aspNetCURD.Database;
using aspNetCURD.Models;
using System.Collections.Generic;
using System.Linq;

namespace aspNetCURD.Services
{
    public class ScRepository : IScRepository
    {
        private readonly AppDbContext _context;
        public ScRepository(AppDbContext context)
        {
            _context = context;
        }

        public IEnumerable<sc> GetScs(string keyword)
        {
            if (!string.IsNullOrEmpty(keyword))
            {
                keyword = keyword.Trim();
                return _context.sc.Where(n =>
                    n.sno.Contains(keyword) ||
                    n.cno.Contains(keyword)).ToList();
            }
            else
            {
                return _context.sc;
            }
        }

        public sc GetSc(string sno)
        {
            return _context.sc.Where(n => n.sno == sno).FirstOrDefault();
        }

        public void AddSc(sc scMod)
        {
            _context.sc.Add(scMod);
        }

        public bool Save()
        {
            return (_context.SaveChanges() >= 0);
        }
    }
}
